package com.papper.common.config;

import com.alibaba.fastjson.JSONObject;
import com.papper.common.bean.sys.SysSystem;
import com.papper.common.service.sys.SysSystemService;
import com.papper.common.utils.PapperSpringUtil;
import com.papper.common.utils.tool.StringTool;

/**
 * @Author:Lajiao
 * @Date:2014年5月20日
 * @Time:下午5:58:45
 * @Description:系统信息
 */
public class SysSystemHelp {
	private static SysSystemHelp instance  = null;
	private static SysSystem     SysSystem = null;
	private static JSONObject    Config    = null;

	private SysSystemHelp() {
	}

	public static synchronized SysSystemHelp I() {
		if (instance == null) {
			instance = new SysSystemHelp();
			instance.initPropertys();
		}
		return instance;
	}

	public void initPropertys() {
		try {
			SysSystemService sysSystemService = (SysSystemService) PapperSpringUtil.getBean("sysSystemService");
			SysSystem = sysSystemService.findSysSystem();
			if (SysSystem != null) {
				Config = JSONObject.parseObject(SysSystem.getSystemDesc());
			} else {
				// 默认配置系统
				SysSystem = new SysSystem();
				SysSystem.setSystemCode(LocalConfig.I().GS(LocalConfig.SYSTEM_CODE));
				SysSystem.setSystemName(LocalConfig.I().GS(LocalConfig.SYSTEM_NAME));
				Config = new JSONObject();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public SysSystem sys() {
		return SysSystem;
	}

	public String config(String key) {
		if (Config == null) {
			return StringTool.EMPTY;
		}
		return StringTool.nullToEmpty(Config.getString(key));
	}
}
